import random
import string
import oss2


def put_object_from_file(file):
    """
    # :param name: 在阿里云Bucket中要保存的文件名
    :param file: 本地图片的文件名
    :return:
    """
    access_key_id = "LTAI5tSQpTmiCiHfS7Y9Uyr7"  # 从阿里云查询到的 AccessKey 的ID
    access_key_secret = "nwQ89UAnT0YsK0ib8poLIfBsC6GiVD"  # 从阿里云查询到的 AccessKey 的Secret
    auth = oss2.Auth(access_key_id, access_key_secret)
    bucket_name = "ihome-django"  # 阿里云上创建好的Bucket的名称
    endpoint = "ihome-django.oss-cn-beijing-internal.aliyuncs.com"  # 阿里云从Bucket中查询到的endpoint
    bucket = oss2.Bucket(auth, endpoint, bucket_name)

    bucket.put_object_from_file(file,)
    return "https://{}.{}/".format(bucket_name, endpoint)

#
# if __name__ == '__main__':
#     # 此处第一个参数 img/katong1.png 中的img是阿里云的Bucket中事先创建好的test文件夹，第二个参数/images/katong1.png是本地的图片katong1.png
#     img_url = AliyunOss().put_object_from_file("test/katong1.png", "/images/katong1.png")
#     print(img_url)
